//
//  GWRaidersViewController.m
//  GiftWanted
//
//  Created by lanou on 15/11/9.
//  Copyright © 2015年 陈文杰. All rights reserved.
//

#import "GWRaidersViewController.h"
#import "GWRaidersModel.h"
#import "MBProgressHUD.h"
#import "GWPurchaseViewController.h"
#import "UIImageView+WebCache.h"


@interface GWRaidersViewController () <UIWebViewDelegate,UIScrollViewDelegate>

@property (nonatomic, strong) GWRaidersModel *model;
@property (nonatomic, strong) UIWebView *webView;

@property (nonatomic, strong) UIImageView *imgProfile;
@property (nonatomic, strong) UILabel *nameLabel;

@end

@implementation GWRaidersViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self.leftButton setBackgroundImage:[UIImage imageNamed:@"return"] forState:(UIControlStateNormal)];
    [self.leftButton addTarget:self action:@selector(handleReturnButtonAction) forControlEvents:(UIControlEventTouchUpInside)];
    self.titleLabel.text = @"攻略";
    
    self.imgProfile = [[UIImageView alloc] initWithFrame:CGRectMake(0, 70, SCREEN_WIDTH, 150)];
    self.imgProfile.contentMode = YES;
    
    self.nameLabel = [[UILabel alloc] initWithFrame:CGRectMake(10, 180, SCREEN_WIDTH-20, 60)];
    _nameLabel.numberOfLines = 2;
    _nameLabel.textAlignment = NSTextAlignmentCenter;
    _nameLabel.textColor = [UIColor whiteColor];
    
    _webView = [[UIWebView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT-70)];
    _webView.scrollView.contentInset = UIEdgeInsetsMake(150, 0, 0, 0);
    _webView.scrollView.delegate = self;
    _webView.delegate = self;
    _webView.backgroundColor = [UIColor clearColor];
    [self.view addSubview:self.imgProfile];
    [self.view addSubview:_nameLabel];
    [self.view addSubview:_webView];
    [self.view bringSubviewToFront:self.topView];
    [self configureData];
}

- (void)scrollViewDidScroll:(UIScrollView *)scrollView {
    CGFloat yOffset = self.webView.scrollView.contentOffset.y;
    //    NSLog(@"%lf", yOffset);
    if (yOffset < 0) {
        
        CGFloat factor = ((ABS(yOffset)+150)*SCREEN_WIDTH)/150;
        CGRect f = CGRectMake(-(factor-SCREEN_WIDTH)/2, 70, factor, 150+ABS(yOffset));
        f.size.height = - yOffset;
        self.imgProfile.frame = f;
        
        CGRect l = CGRectMake(10, 180, SCREEN_WIDTH-20, 60);
        l.origin.y = - yOffset+10;
        self.nameLabel.frame = l;
        
    } else {
        
        CGRect f = CGRectMake(0, 70, SCREEN_WIDTH, 150);
        f.origin.y = -yOffset-150;
        self.imgProfile.frame = f;
        
        CGRect l = CGRectMake(10, 180, SCREEN_WIDTH-20, 60);
        l.origin.y = -yOffset-100+10;
        self.nameLabel.frame = l;
    }
    
}

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    
    
    self.webView.frame = CGRectMake(0, 70, SCREEN_WIDTH, SCREEN_HEIGHT-70);
}

- (void)configureData {
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    hud.mode = MBProgressHUDAnimationFade;
    hud.labelText = @"连接中…";
    NSString *urlStr = [@"http://api.liwushuo.com/v2/posts/" stringByAppendingFormat:@"%@", _id_s];
    AFHTTPRequestOperation *operation = [[AFHTTPRequestOperation alloc] initWithRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:urlStr]]];
    [operation setCompletionBlockWithSuccess:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSDictionary *dict = [NSJSONSerialization JSONObjectWithData:responseObject options:NSJSONReadingMutableContainers|NSJSONReadingMutableLeaves error:nil];
        GWRaidersModel *model = [[GWRaidersModel alloc] init];
        [model setValuesForKeysWithDictionary:dict[@"data"]];
        
        [_imgProfile sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
        _nameLabel.text = model.title;
        
        NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:model.content_url]];
        [_webView loadRequest:request];
        
        [MBProgressHUD hideHUDForView:self.view animated:YES];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"网络请求失败");
    }];
    [operation start];
}

- (BOOL)webView:(UIWebView *)webView shouldStartLoadWithRequest:(NSURLRequest *)request navigationType:(UIWebViewNavigationType)navigationType {
    
    NSLog(@"%@", request.URL.absoluteString);
    if ([request.URL.absoluteString isEqualToString:[@"http://www.liwushuo.com/posts/" stringByAppendingFormat:@"%@%@", _id_s, @"/content"]]) {
        return YES;
    } else {
        GWPurchaseViewController *purchaseVC = [[GWPurchaseViewController alloc] init];
        purchaseVC.purchase_url = request.URL.absoluteString;
        [self.navigationController pushViewController:purchaseVC animated:YES];
        return NO;
    }
}


- (void)handleReturnButtonAction {
    
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
